import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:my_flutter_tool/util/app_util.dart';
import 'package:my_flutter_tool/util/log_utils.dart';

import 'test_page.dart';

///设置界面
class SettingPage extends StatefulWidget {
  const SettingPage({Key key}) : super(key: key);

  @override
  _SettingPageState createState() => _SettingPageState();
}

class _SettingPageState extends State<SettingPage> {
  ScrollController _controller;

  @override
  void initState() {
    super.initState();

    _controller = ScrollController();
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    L.w('rebuild SettingPage');

    const List<Color> c = [
      Colors.red,
      Colors.green,
      Colors.pink,
      Colors.purple,
      Colors.amber,
      Colors.blue,
      Colors.black,
    ];

    return Scaffold(
      appBar: AppBar(
        leading: IconButton(
            icon: Icon(Icons.arrow_back),
            onPressed: () async {
              A.back(context);
            }),
        title: Text('Settings'),
      ),
      body: ListView(
        physics: A.appPhysics,
        children: [
          SizedBox(
            height: 50,
            child: ListView.separated(
              scrollDirection: Axis.horizontal,
              itemCount: c.length,
              itemBuilder: (_, index) {
                return FlatButton(
                  color: c[index],
                  onPressed: () {
                    Get.changeTheme(ThemeData.light().copyWith(
                      primaryColor: c[index],
                      accentColor: c[index],
                    ));
                  },
                  child: Text('${c[index].value}'),
                );
              },
              separatorBuilder: (_, __) {
                return const SizedBox(width: 20);
              },
            ),
          ),
          DefaultTabController(
            length: 10,
            child: SizedBox(
              height: 100,
              child: NotificationListener(
                  onNotification: (ScrollNotification n) {
                    L.i('content position :${n.metrics.pixels}');
                    return true;
                  },
                  child: ListView(
                    controller: _controller,
                    padding: EdgeInsets.only(
                        right: MediaQuery.of(context).size.width / 2),
                    scrollDirection: Axis.horizontal,
                    children: [
                      TabBar(
                        onTap: (i) async {
                          await _controller.animateTo(
                            i * 50.0,
                            duration: const Duration(milliseconds: 300),
                            curve: Curves.ease,
                          );
                        },
                        isScrollable: true,
                        labelColor: Colors.black,
                        labelPadding: EdgeInsets.zero,
                        tabs: [
                          Tab(text: 'TAB1'),
                          Tab(text: 'TAB2'),
                          Tab(text: 'TAB3'),
                          Tab(text: 'TAB4'),
                          Tab(text: 'TAB5'),
                          Tab(text: 'TAB6'),
                          Tab(text: 'TAB7'),
                          Tab(text: 'TAB8'),
                          Tab(text: 'TAB9'),
                          Tab(text: 'TAB10'),
                        ],
                      ),
                    ],
                  )),
            ),
          ),
          FlatButton(
            onPressed: () {
              A.jump(context, const TestPage());
            },
            child: Text('scroll tab'),
          )
        ],
      ),
    );
  }
}
